package cn.yzgy.yzgyoa.ucenter.mapper;

import cn.yzgy.yzgyoa.ucenter.dto.UserDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-20
 */
public interface SysUserMapper extends BaseMapper<SysUser> {
    /**
     * 查询用户相关数据
     * @param wrapper 查询条件集
     * @param num 从第num条数据开始查询
     * @param size 查询size条数据
     * @return map集合
     */
    @Select("select* from (SELECT a.uid AS showId,a.login_name AS loginName,a.state AS state,b.NAME AS NAME," +
            "b.phone AS phone,b.data_perm AS dataPerm,b.gender AS gender,b.oid as oid,GROUP_CONCAT(d.role_name) AS roleNames," +
            "GROUP_CONCAT(d.role_remarks) AS roleRemarks,e.NAME AS groupName,GROUP_CONCAT(c.role_id) AS roleId " +
            "FROM `sys_user` a,sys_user_detail b,sys_user_role c,sys_role d,sys_organization e " +
            "WHERE a.uid=b.uid AND a.uid=c.uid AND c.role_id=d.role_id AND b.oid=e.oid " +
            "GROUP BY a.uid,a.login_name,a.state,b.NAME,b.phone,b.data_perm,b.gender,e.NAME) h ${ew.customSqlSegment} LIMIT ${num},${size}")
    List<UserDTO> queryAll(@Param(Constants.WRAPPER) Wrapper wrapper,@Param("num")Integer num, @Param("size")Integer size);

    /**
     * 获取展示数据总条数
     * @return 获取展示数据总条数
     */
    @Select("select count(1) from (SELECT a.uid AS showId,a.login_name AS loginName,a.state AS state,b.NAME AS NAME," +
            "b.phone AS phone,b.data_perm AS dataPerm,b.gender AS gender,b.oid as oid,GROUP_CONCAT(d.role_name) AS roleNames," +
            "GROUP_CONCAT(d.role_remarks) AS roleRemarks,e.NAME AS groupName,GROUP_CONCAT(c.role_id) AS roleId " +
            "FROM `sys_user` a,sys_user_detail b,sys_user_role c,sys_role d,sys_organization e " +
            "WHERE a.uid=b.uid AND a.uid=c.uid AND c.role_id=d.role_id AND b.oid=e.oid " +
            "GROUP BY a.uid,a.login_name,a.state,b.NAME,b.phone,b.data_perm,b.gender,e.NAME ) h ")
    int countData();


    /**
     * 查重
     * @param wrapper 查询条件集
     * @return 重复对象
     */
    @Select("select * from sys_user  ${ew.customSqlSegment} ")
    SysUser check(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 重置密码
     * @param psw 密码
     * @param salt 加密方式
     * @param wrapper 重置条件集
     * @return 修改数量
     */
    @Update("update sys_user set psw='${psw}',salt='${salt}' ${ew.customSqlSegment}")
    int change(@Param("psw") String psw,@Param("salt") String salt,@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 停用用户
     * @param wrapper 条件集
     * @return 修改条数
     */
    @Update("update sys_user set state='0' ${ew.customSqlSegment}")
    int stopState(@Param("ew") Wrapper wrapper);

    /**
     * 启用用户
     * @param wrapper 条件集
     * @return 修改条数
     */
    @Update("update sys_user set state='1'${ew.customSqlSegment}")
    int startState(@Param("ew") Wrapper wrapper);

}
